package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcExpenseEntity;
import com.dhcc.cdc.busi.service.ICdcExpenseService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 费用
 * 
 * @ClassName: CdcExpenseController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:08
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcexpense")
public class CdcExpenseController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcexpense/";

	@Autowired
	private ICdcExpenseService cdcExpenseService;
	

    /**
     * 跳转到费用列表首页
     */
    @RequestMapping("")
    @Permission("cdcexpense:list")
    public String index() {
        return PREFIX + "cdcexpense.html";
    }
	
    /**
     * 跳转到新增费用
     */
    @RequestMapping("/to_add")
    @Permission("cdcexpense:add")
    public String toAdd() {
        return PREFIX + "cdcexpense_add.html";
    }
	
	/**
     * 跳转到修改费用
     */
    @RequestMapping("/to_update/{cdcExpenseId}")
    @Permission("cdcexpense:update")
    public String toUpdate(@PathVariable Long cdcExpenseId, Model model) {
        CdcExpenseEntity cdcExpense = this.cdcExpenseService.selectById(cdcExpenseId);
        model.addAttribute("cdcExpense",cdcExpense);
        return PREFIX + "cdcexpitem_edit.html";
    }

    /**
     * 获取费用列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcexpense:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcExpenseEntity> page = new PageFactory<CdcExpenseEntity>().defaultPage();

		List<CdcExpenseEntity> result=null;
		if((CommonUtil.isNotEmpty(params.get("NotQuery")))&&params.get("NotQuery").toString().equals("true")){
            result=new ArrayList<CdcExpenseEntity>();
        }else{
            result =  this.cdcExpenseService.page(page,params);
        }


        page.setRecords((List<CdcExpenseEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增费用
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcexpense:add")
    @BussinessLog(value = "新增费用")
    public Object add(CdcExpenseEntity cdcExpense) {
        if (CommonUtil.isOneEmpty(cdcExpense)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcExpense.insert();
        return R.ok();
    }

    /**
     * 删除费用
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcexpense:delete")
    @BussinessLog(value = "删除费用")
    public Object delete(@RequestParam Long cdcExpenseId) {
	
        this.cdcExpenseService.deleteById(cdcExpenseId);

        return R.ok();
    }

    /**
     * 修改费用
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcexpense:update")
    @BussinessLog(value = "修改费用")
    public Object update(CdcExpenseEntity cdcExpense) {
        if (CommonUtil.isOneEmpty(cdcExpense)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcExpenseEntity old = this.cdcExpenseService.selectById(cdcExpense.getId());

        old.setEpisodeId(cdcExpense.getEpisodeId());
        old.setCosts(cdcExpense.getCosts());
        old.setPays(cdcExpense.getPays());
        old.setBusExpenseTypeId(cdcExpense.getBusExpenseTypeId());
        old.setDataId(cdcExpense.getDataId());
        old.setIsActive(cdcExpense.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
